IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_PageControlsReOrder]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_PageControlsReOrder]
GO
create procedure [dbo].[cms_sp_PageControlsReOrder]
(
    @TemplateRegionId int,
	@PageId int
)
as
begin
declare @ControlOrder int
declare @PageControlId int
declare page_control_reorder_cursor cursor for 
select 
	PageControlId
from dbo.PageControls
where TemplateRegionId = @TemplateRegionId and PageId = @PageId	
order by PageControlOrder
set @ControlOrder = 1
open page_control_reorder_cursor;
fetch next from page_control_reorder_cursor 
	into @PageControlId
while @@fetch_status = 0
begin
update dbo.PageControls
	set PageControlOrder = @ControlOrder
where PageControlId = @PageControlId
set @ControlOrder = @ControlOrder + 1
fetch next from page_control_reorder_cursor 
	into @PageControlId
end
close page_control_reorder_cursor
deallocate page_control_reorder_cursor
end


GO

